package com.haiqi.modules.report.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.AccidentReport;
import com.haiqi.common.utils.DateUtils;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.report.service.AccidentReportService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by hythzx on 2016/5/24.
 * 事故险情报告
 */
@Controller
@RequestMapping("/manage/report/accident/report")
@SafeAuthority(menuCode = "accidentReport,monitorLocation")
public class AccidentReportController extends BaseController{

    @Autowired
    private AccidentReportService accidentReportService;

    /**
     * 本菜单的code
     */
    private String menuCode = "accidentReport";



    @RequestMapping("/list")
    public String list(AccidentReport accidentReport, PageBean page, Model model, HttpSession session){
        generateMenuCode(session,menuCode);
        ResponseBean.UserBean user = getUser(session);
        accidentReport.setCompany(user.getOfficeid());
        PageInfo<AccidentReport> pageInfo = accidentReportService.getList(accidentReport, page);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("param",accidentReport);
        return "/modules/report/accidentReport";
    }


    @ResponseBody
    @RequestMapping("/ajax/list")
    public DataGridBean<AccidentReport>  ajaxList(AccidentReport accidentReport, PageBean page, Model model, HttpSession session,String company){
        if(StringUtils.isNotEmpty(company)){
            accidentReport.setCompany(company);
        }
        PageInfo<AccidentReport> pageInfo = accidentReportService.getList(accidentReport, page);
        DataGridBean<AccidentReport> accidentReportDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, accidentReportDataGridBean);
        if(accidentReportDataGridBean.getRows() != null){
            for (AccidentReport report : accidentReportDataGridBean.getRows()) {
                report.setDateReportStr(DateUtils.dateToString(report.getDateReport(), DateUtils.DATE_FORMAT));
                report.setAccidentTimeStr(DateUtils.dateToString(report.getAccidentTime(),DateUtils.DATEMINUTE_FORMAT));
            }
        }
        return accidentReportDataGridBean;
    }

}
